package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.NewsFindAllDao;
import com.cn.wanxi.model.NewsModel;

import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsFindAllImpl implements NewsFindAllDao {
    @Override
    public List<NewsModel> getNewsFindAll(NewsModel newsModel) {
        String sql = "select * from news where enable = 1  ";
        if (newsModel.getType() != null && !"".equals(newsModel.getType())) {
            sql += " and type = '" + newsModel.getType() + "' ";
        }
        sql += " order by id asc";
        ResultSet resultSet = JDBC.query(sql);
        List<NewsModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                NewsModel model = new NewsModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setContent(resultSet.getString("content"));
                model.setTitle(resultSet.getString("title"));
                model.setCreateTime(resultSet.getString("create_time"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setWhereShow(resultSet.getInt("where_show"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<NewsModel> getNewsInformationById(NewsModel newsModel) {
        String sql = "(SELECT * FROM news where id=(select max(id) from news where id<"+newsModel.getId()+"))";
        sql += "union all";
        sql += "(SELECT * FROM news where id="+newsModel.getId()+")";
        sql += "union all";
        sql += "(SELECT * FROM news where id=(select min(id) from news where id>"+newsModel.getId()+"))";
        ResultSet resultSet = JDBC.query(sql);
        List<NewsModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                NewsModel model = new NewsModel();
                model.setId(resultSet.getInt("id"));
                model.setTitle(resultSet.getString("title"));
                model.setContent(resultSet.getString("content"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setCreateTime(resultSet.getString("create_time"));
                model.setEnable(resultSet.getInt("enable"));
                model.setName(resultSet.getString("name"));
                model.setType(resultSet.getInt("type"));
                model.setSort(resultSet.getInt("sort"));
                model.setWhereShow(resultSet.getInt("where_show"));
                model.setAuthor(resultSet.getString("author"));
                model.setSubscribe(resultSet.getInt("subscribe"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<NewsModel> findAll(NewsModel newsModel) {
        return null;
    }

    @Override
    public int add(NewsModel newsModel) {
        return 0;
    }

    @Override
    public int update(NewsModel newsModel) {
        return 0;
    }

    @Override
    public NewsModel findById(NewsModel newsModel) {
        String sql = "select * from news where id =" + newsModel.getId();
        ResultSet resultSet = JDBC.query(sql);
        NewsModel model = null;
        try {
            while (resultSet.next()) {
                model = new NewsModel();
                model.setId(resultSet.getInt("id"));
                model.setTitle(resultSet.getString("title"));
                model.setContent(resultSet.getString("content"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setCreateTime(resultSet.getString("create_time"));
                model.setEnable(resultSet.getInt("enable"));
                model.setName(resultSet.getString("name"));
                model.setType(resultSet.getInt("type"));
                model.setSort(resultSet.getInt("sort"));
                model.setWhereShow(resultSet.getInt("where_show"));
                model.setAuthor(resultSet.getString("author"));
                model.setSubscribe(resultSet.getInt("subscribe"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int delete(NewsModel newsModel) {
        return 0;
    }

    @Override
    public int getCount(NewsModel newsModel) {
        return 0;
    }
}
